from Tkinter import *
from tkMessageBox import *
from svn_interface import *

class PickBranchUI(object):
  def __init__(self, master, branches, callback):
    self.callback = callback
    self.prompt = prompt = Toplevel(master)
    prompt.title("Choose Commit")
    # TODO: scrolled list box
    self.label = Label(prompt, text="Pick a commit to merge:")
    self.label.pack(side=TOP, fill=X, expand=1)

    self.list_frame = Frame(prompt)
    vscroll = Scrollbar(self.list_frame, orient=VERTICAL)
    self.commit_list = Listbox(self.list_frame, width=40, height=10, yscrollcommand=vscroll.set)
    self.commit_list.pack(side=LEFT, fill=BOTH, expand=1)
    vscroll.config(command=self.commit_list.yview)
    vscroll.pack(side=RIGHT, fill=Y)
    self.list_frame.pack(side=TOP, fill=BOTH, expand=1)

    btn = Button(prompt, text="Merge", command=self.on_pick)
    btn.pack(side=RIGHT)
    btn = Button(prompt, text="Cancel", command=prompt.destroy)
    btn.pack(side=LEFT)
    for branch in branches:
      self.commit_list.insert(END, branch)
    self.branches = branches

    # TODO: this freezes the GUI thread. Technically, it makes the
    # dialog box modal, though!

  def on_pick(self):
    chosen_branch = None
    for item in self.commit_list.curselection():
      chosen_branch = self.branches[int(item)]
    if chosen_branch is None:
      showerror("No Commit Chosen", "Please click a commit to merge!")
      return
    self.prompt.destroy()
    self.callback(chosen_branch)

